-- to_layer
-- Created by chengb Oct/20/2015
-- 成就：到达迷宫指定层

-- 内部函数声明
local whenNextFloor;

-- 关注玩家进入下一层的回调
EventMgr.register("google_achievement/to_layer", event.NEXT_FLOOR, function()
    whenNextFloor();
end);

-- 关注玩家进入下一层的回调
whenNextFloor = function()
    local layer = DungeonM.currentLayer();

    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == GoogleAchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end
        
        local config = GoogleAchievementM.query(id);
        local ruleArg = config["rule_arg"];
        if type(ruleArg) == 'table' then
            if type(ruleArg["layer"]) == 'number' and
                ruleArg["layer"] ~= layer then
                -- 层数不匹配
                return false;
            end
        end

        return true;
    end

    -- 遍历所有的子规则
    local ids = GoogleAchievementM.getIdsByRule("to_layer");
    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            GoogleAchievementM.tryComplete(id, 1);
        end
    end
end

